define(['jquery', 'bootstrap','backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'active/index/index',
                    add_url: 'active/index/add',
                    edit_url: 'active/index/edit',
                    del_url: 'active/index/del',
                    multi_url: '',
                    table: 'page',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'weight',
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'title', title: __('title'),editable:"input",operate: false},
                        {field: 'details', title: __('details'),operate:false},
                        {field: 'weight', title: __('weight'),operate:false},
                        {field: 'img', title: __('img'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'begintime', title: __('begintime'),  operate:false},
                        {field: 'endtime', title: __('endtime'),  operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ],
                //普通搜索
                commonSearch: false,
                searchFormVisible: false,
                titleForm: '', //为空则不显示标题，不定义默认显示：普通搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //刷新表单
            $(document).on("click", ".btn-refreshpic", function () {
                table.bootstrapTable('refresh');
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        addworklog: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                operate: function (value, row, index) {
                    //返回字符串加上Table.api.formatter.operate的结果
                    //默认需要按需显示排序/编辑/删除按钮,则需要在Table.api.formatter.operate将table传入
                    //传入了table以后如果edit_url为空则不显示编辑按钮,如果del_url为空则不显显删除按钮
                    return '<a class="btn btn-info btn-xs btn-detail"><i class="fa fa-user"></i> ' + '预约列表' + '</a> '
                        + Table.api.formatter.operate(value, row, index, $("#table"));
                },
            },
            events: {
                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        Backend.api.open('active/activelist/index/id/' + row['id'], __('Detail'));
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});